package com.cheer.ba.view.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.andview.refreshview.XRefreshView;
import com.bigkoo.svprogresshud.SVProgressHUD;
import com.cheer.ba.R;
import com.cheer.ba.cache.preference.Session;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.H5Address;
import com.cheer.ba.model.PlayerModel;
import com.cheer.ba.presenter.impl.PlayerPresenter;
import com.cheer.ba.utils.DensityUtils;
import com.cheer.ba.utils.Protect;
import com.cheer.ba.utils.ShareNewsUtil;
import com.cheer.ba.utils.SystemUtils;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.ba.view.base.CommonAdapter;
import com.cheer.ba.view.base.ViewHolder;
import com.cheer.ba.widget.headview.CheerHeadView;
import com.umeng.socialize.UMShareAPI;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 优惠活动界面
 *
 * @author HQ
 *         created at 2017/4/1 0001 14:57
 */
public class FreePlayAty extends BaseCommAty implements AdapterView.OnItemClickListener {

    @BindView(R.id.id_hom_play_xrefreshv)
    XRefreshView mXRefreshView;
    @BindView(R.id.id_hom_play_listv)
    ListView mListView;
    CommonAdapter<PlayerModel> mPlayAdp;
    private List<PlayerModel> mPlays = new ArrayList<PlayerModel>();

    private ShareNewsUtil mShare;

    //活动图宽度
    private int activity_height;

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.frg_player, null, false));
    }

    @Override
    public boolean hasDataInPage() {
        return !mPlays.isEmpty();
    }

    @Override
    public void initPresenter() {
        presenter = new PlayerPresenter();
    }

    @Override
    public void initView() {
        setTitleText(getString(R.string.free_play));
        setXRefreshView(mXRefreshView, true, false, false, false, new CheerHeadView(aty), null);

        int width = SystemUtils.getScreenWidth(aty) - DensityUtils.dip2px(aty, 10);
        activity_height = (int) (202 * width / 710);
        mPlayAdp = new CommonAdapter<PlayerModel>(aty, mPlays, R.layout.adp_players) {
            @Override
            public void convert(ViewHolder helper, PlayerModel item) {
                if (null != item) {
                    ImageView img = (ImageView) helper.getViewById(R.id.id_players_img);
                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, activity_height);
                    img.setLayoutParams(params);
                    helper.setRoundImageByUrl(R.id.id_players_img, Protect.setPic(item.getBackImg()), R.drawable.ic_error_show_bg, aty);
                    helper.setText(R.id.id_game_content_txt, item.getLotteryName());
                    //活动是否截至
                    ImageView mImgEnd = (ImageView) helper.getViewById(R.id.id_game_img);
                    mImgEnd.setVisibility(View.VISIBLE);
                    //当前日期
                    long currentTime = System.currentTimeMillis();
                    if (currentTime >= item.getStartDate() && currentTime < item.getEndDate()) {
                        mImgEnd.setVisibility(View.GONE);
                    } else if (currentTime < item.getStartDate()) {
                        //活动未开始
                        mImgEnd.setBackgroundResource(R.drawable.ic_play_no_begin);
                    } else {
                        //活动已结束
                        mImgEnd.setBackgroundResource(R.drawable.ic_game_over);
                    }
                }
            }
        };
        mListView.setAdapter(mPlayAdp);
        mListView.setOnItemClickListener(this);
    }

    @Override
    public void initData() {
        mShare = new ShareNewsUtil(aty, "为了省钱的名义！", "加油省钱？选加油啦错不了！", H5Address.SHARE_APP, "");
        setTitleIcon(R.drawable.ic_sharer, false, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mShare.showShare();
            }
        });

        mXRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(final boolean isPullDown) {
                loadData(false);
            }
        });
        if (hasDataInPage()) {
            mXRefreshView.startRefresh();
        } else {
            loadData(true);
        }
    }

    /**
     * 加载数据
     */
    public void loadData(boolean isShowLoad) {
        ((PlayerPresenter) presenter).getPlayers(isShowLoad);
        stopRefresh(mXRefreshView, 2000);
    }

    @Override
    public <T> void updateViewWithTag(T t, String tag) {
        mXRefreshView.stopRefresh();
        if (null != t) {
            if (TextUtils.equals(tag, ConstantTag.OIL_PLAYERS)) {
                List<PlayerModel> plays = (List<PlayerModel>) t;
                mPlays.clear();
                mPlays.addAll(plays);
                mPlayAdp.notifyDataSetChanged();
            } else if (TextUtils.equals(tag, ConstantTag.DATA_SUCCESS_NULL)) {
                //没有数据
                mPlays.clear();
                mPlayAdp.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (isLogin(true)) {
            PlayerModel item = (PlayerModel) mPlayAdp.getItem(position);
            //当前日期
            long currentTime = System.currentTimeMillis();
            if (currentTime >= item.getStartDate() && currentTime < item.getEndDate()) {
                String url = H5Address.GET_PRIZE + "custId=" + Session.getUserId() + "&lottery_id=" + item.getUid();
                CommH5WithTitleAty.startCommonH5(aty, url, "抽奖", false, false, "", "", "");
            } else if (currentTime < item.getStartDate()) {
                //活动未开始
                SVProgressHUD.showInfoWithStatus(aty, "该活动还未开始");
            } else {
                //活动已结束
                SVProgressHUD.showInfoWithStatus(aty, "该活动已结束");
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /** attention to this below ,must add this**/
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}
